#!/usr/bin python3
# -*- coding: utf-8 -*-
"""  用户相关操作dao   """
from gtm_server.models.users import UserModel
from gtm_server.common.constant import TaskStatusKeys

def register_user_dao(session, id, account, name, password, part, company, title):
    """
    用户注册
    """
    try:
        new_user = UserModel(
            id = id,
            account = account,
            name = name,
            password = password,
            part = part,
            company = company,
            title = title
        )

        session.add(new_user)
        session.commit()
    except Exception as ex:
        return TaskStatusKeys.FAILED

    return TaskStatusKeys.SUCCESS


def get_user_by_account(session, account):
    """
    根据account获取用户信息
    """
    try:
        user = session.query(UserModel).filter(UserModel.account == account).one_or_none()
        return user
    except Exception as ex:
        print(ex)
        return TaskStatusKeys.FAILED


def get_all_account(session):
    """
    获取所有的账户列表
    """
    try:
        users = session.query(UserModel).all()
        if users:
            return [user for user in users]
        else:
            return None
    except Exception as ex:
        print(ex)
        return TaskStatusKeys.FAILED